//
//  XGNewsListTableViewController.swift
//  TodayNews
//
//  Created by monkey on 2020/3/25.
//  Copyright © 2020 itcast. All rights reserved.
//

import UIKit
import JXSegmentedView
import MJRefresh

protocol XGNewsListTableViewControllerDelegate: AnyObject
{
    func newsDidSelected(newsViewModel: XGNewsViewModel) -> Void
}

class XGNewsListTableViewController: UITableViewController,JXSegmentedListContainerViewListDelegate
{
    /// 代理
    weak var newsDelegate: XGNewsListTableViewControllerDelegate?
    
    func listView() -> UIView
    {
        return view
    }
    
    init(newsCategory: XGNewsCategoryModel)
    {
        self.newsCategory = newsCategory
        super.init(style: .plain)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
       
    // MARK: - 控制器生命周期方法
    
    override func viewDidLoad()
    {
        super.viewDidLoad()
        setUpTableView()
        loadData()
    }
    
    // MARK: - 私有属性
    
    /// 新闻分类模型
    private let newsCategory: XGNewsCategoryModel
    /// 视图模型
    private let viewModel = XGHomeViewModel()
}

// MARK: - 数据源和代理方法

extension XGNewsListTableViewController
{
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        return viewModel.newsList.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        var cell: XGNewsProtocol & UITableViewCell
        let newsViewModel = viewModel.newsList[indexPath.row]
        switch newsViewModel.cellType { // 判断cell类型
        case .singlePicture:
            cell = tableView.xg_dequeueReusableCell(indexPath: indexPath) as XGSinglePictureNewsCell
        case .video:
            cell = tableView.xg_dequeueReusableCell(indexPath: indexPath) as XGVideoNewsCell
        default:
            cell = tableView.xg_dequeueReusableCell(indexPath: indexPath) as XGNormalNewsCell
        }
        
        cell.newsViewModel = viewModel.newsList[indexPath.row]
        cell.theme_backgroundColor = "colors.cell_background"
        return cell
    }
    
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
    {
        return viewModel.newsList[indexPath.row].rowHeight
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
    {
        newsDelegate?.newsDidSelected(newsViewModel: viewModel.newsList[indexPath.row])
    }
}

// MARK: - 私有扩展

private extension XGNewsListTableViewController
{
    /// 加载数据
    func loadData(isLoadMore: Bool = false) -> Void
    {
        guard let category = newsCategory.category else {
            return
        }
        
        viewModel.fetchNewsList(category: category, isLoadMore: isLoadMore) { (isSuccess) in
            if isSuccess {
                if self.tableView.mj_header?.isRefreshing == true {
                    self.tableView.mj_header?.endRefreshing()
                }
                if self.tableView.mj_footer?.isRefreshing == true {
                    self.tableView.mj_footer?.endRefreshing()
                }
                
                self.tableView.reloadData()
            }
        }
    }
    
    func setUpTableView() -> Void
    {
        tableView.xg_registerCell(cellType: XGNormalNewsCell.self)
        tableView.xg_registerCell(cellType: XGSinglePictureNewsCell.self)
        tableView.xg_registerCell(cellType: XGVideoNewsCell.self)
        tableView.separatorInset = UIEdgeInsets.zero
        tableView.tableFooterView = UIView()
        tableView.mj_header = MJRefreshNormalHeader(refreshingBlock: { [weak self] in
            self?.loadData(isLoadMore: false)
        })
        tableView.mj_footer = MJRefreshAutoNormalFooter(refreshingBlock: { [weak self] in
            self?.loadData(isLoadMore: true)
        })
    }
}
